import json
import unittest
import ddt
import requests

from Auto_excelInterfaceTest.common_utils.commons import Common
from Auto_excelInterfaceTest.common_utils.logging import LogHandler

r = Common().ReadExcelTypeDict('superviseCase.xlsx')


@ddt.ddt
class Auto_excelSuperviseTest(unittest.TestCase):
    @ddt.data(*r)
    def test_superviseTest(self, value):
        """事件核查督促检索框检查"""
        log = LogHandler("main")
        url1 = value['登录接口地址']
        url2 = value['督促框接口地址']
        superviseStatus = value['督促状态']
        dic = json.loads(value['post参数值'])
        self.session = requests.session()
        self.session.post(url=url1, data=dic)
        log.info('登录成功,登录参数值为：{data}'.format(data=value['post参数值']))
        response = self.session.get(url=url2)
        result = response.json()
        if superviseStatus == '未督促':
            log.info("查看未督促数据：")
            if len(result['data']['list']) == 0:
                t1 = False
                if t1 is False:
                    log.error("数据为空，无法判断！")
                self.assertTrue(t1, msg="数据为空，无法判断！")
            for item in result['data']['list']:
                t = 'superviseStatus' in item
                if t is True:
                    log.error("存在已督促数据：{data}，督促类型为：{status}".format(data=item['eventName'], status=item['superviseStatus']))
                self.assertFalse(t, msg="存在已督促数据：" + item['eventName'] + "督促类型为：" + item['superviseStatus'])
                log.info("{name}：未督促".format(name=item['eventName']))
        else:
            log.info("查看已督促数据：")
            if len(result['data']['list']) == 0:
                t1 = False
                if t1 is False:
                    log.error("数据为空，无法判断！")
                self.assertTrue(t1, msg="数据为空，无法判断！")
            for item in result['data']['list']:
                t = 'superviseStatus' in item
                if t is False:
                    log.error("存在未督促数据：{data}".format(data=item['eventName']))
                self.assertTrue(t, msg="存在未督促数据： " + item['eventName'])
                log.info("{name}：已督促".format(name=item['eventName']))
        self.session.close()


if __name__ == "__main__":
    Auto_excelSuperviseTest()
